package asteroids.model.programs.binaryexpression;

import asteroids.Util;
import asteroids.model.SpaceObject;
import asteroids.model.programs.Expression;
import asteroids.model.programs.Program;
import exception.IllegalOperandException;


public class Equality extends BinaryExpression<Boolean> {


	public Equality(Expression<?> left, Expression<?> right)
			throws IllegalOperandException {
		super(left, right);
	}


	@Override
	public Boolean getValue(Program program) {
		if(getLeftOperand().getValue(program) == null && getRightOperand().getValue(program) == null)
			return true;
		else if(getLeftOperand().getValue(program).getClass()==Double.class){
			return Util.fuzzyEquals((Double)(getLeftOperand().getValue(program)),(Double)(getRightOperand()).getValue(program));
		}
		else if(getLeftOperand().getValue(program)==Boolean.class){
			if(((Boolean)(getLeftOperand()).getValue(program)) == ((Boolean)(getLeftOperand()).getValue(program)))
				return true;
		}
		else if(getLeftOperand().getValue(program)!=null && getLeftOperand().getValue(program) instanceof SpaceObject){
			if(((SpaceObject)(getLeftOperand()).getValue(program)) == ((SpaceObject)(getRightOperand()).getValue(program)))
				return true;
		}
		return false;
	}


}
